<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class OrderController extends AdminbaseController {
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Order");
		$this->base_price=C("SITE_FUWU_PRICE");
		//$this->terms_relationship = D("Common/TermRelationships");
	}
	
	function index(){


		$starttime = strtotime(I('get.starttime'));

		$endtime = strtotime(I('get.endtime'));

		$map [modal] =array("neq" ,"chongzhi");
		if ($starttime || $endtime) {


			if (!$starttime) {
				$this->error("请填写开始时间");

			}


			if (!$starttime) {
				$this->error("请填写结束时间");
			}

			if ($starttime > $endtime) {
				$this->error("开始时间不能大于结束时间");


			}


			$map['inputtime'] = array('between', array($starttime, $endtime));

		}



		$status=I("get.status")?I("get.status"):2;


		$shop_id=I('get.shop_id');
		if($shop_id){
			$map["shop_id"]=$shop_id;


		}

		$this->assign("status",$status);
		
		if($status) $map['status']=$status;
		$map["modal"]=array("neq","chongzhi");
		
		$phone=I("get.phone");
		
		$this->assign("phone",$phone);
		
		if($phone) $map['tel']=$phone;
		

		if($status==5){
			$count=$this->db->where($map)->count();


			$page = $this->page($count, 10);



			$list=$this->db->where($map)->limit($page->firstRow . ',' . $page->listRows)->order ( "id desc")->select();
			$this->assign("Page",$page->show(""));

		}else{



			$list=$this->db->where($map)->order ( "id desc")->select();


		}
		


		
		
	

		
		$this->assign("list",$list);
	
		
		$this->display("");
	}
	
	
	function news(){
	
	$map['status']=2;
	$map['is_read']=0;
	
	
	
	
	
		$list=$this->db->where($map)->order ( "id asc")->select();
	

	
	
		$this->assign("list",$list);
	
	
		$this->display("");
	}
	
	
	function show(){

		
		$id=  intval(I("get.id"));
		
		$this->db->where("id=$id")->save(array("is_read"=>1));
		
			$rs=$this->db->where("id=$id")->find();

			
			$p_list=D("OrderInfo")->where(array("order_sn"=>$rs["order_sn"]))->select();


		foreach($p_list as $k=>$v){
			$s=D("Product")->where(array("id"=>$v["product_id"]))->find();

			$p_list[$k]["product_title"]=$s["title"];
			$p_list[$k]["p_id"]=$s["p_id"];
			$p_list[$k]["author"]=$s["author"];
			$p_list[$k]["dengji"]=$s["dengji"];

		}


		$yhj=D("Yhj")->where(array("code"=>$rs["yhj"]))->find();



			$this->assign("yhj",$yhj);
			$this->assign("p_list",$p_list);

			$this->assign("_data",$rs);
			
			
			$this->display("");
		
	}
	
	function edit(){
	
	
	
	
	
	
		if(IS_POST){
	
				
			if ($this->db->create()) {
	
				if ($this->db->save()!==false) {
					$this->success("修改成功！", U("index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
	
		}else{
			$id=  intval(I("get.id"));
	
	
	
			$rs=$this->db->where("id=$id")->find();
			$rs['p_list']=D("Peijian")->where(array("id"=>array("in",$rs['pts'])))->select();
	
				
			$this->assign("_data",$rs);
				
				
			$this->display("");
		}
	
	
	
	
	
	
	
	
	}
	
	
	function fahuo(){
		
		
	
		
		
		
			if(IS_POST){
				
				
		
			
					$_POST[status]=3;
					
					
				if ($this->db->create()) {
		
					if ($this->db->save()!==false) {

						$rs=$this->db->where(array("id"=>intval(I("post.id"))))->find();

//						dump($rs);

						$open_id=D("Users")->where(array("id"=>$rs["uid"]))->getField("open_id");
//
//						echo $open_id;
						$content=array(

							"first"=>array(
								"value"=>"您订购的商品已经发货!"
							),
							"keyword1"=>array(
								"value"=>$rs['kuaidi']
							),

							"keyword2"=>array(
								"value"=>$rs['kuaidihao']
							),
							"keyword3"=>array(
								"value"=>$rs['title']
							),
							"keyword4"=>array(
								"value"=>$rs['order_sn']
							),
							"remark"=>array(
								"value"=>"您订购的商品已经发货!"
							),



						);
//
//
//
						$ss=$this->wk->sendTplMsg($open_id,"68YKLL9SexgBfxPboHn2YPdOVfzT-Aq9GkqHe6WOnVg",U('User/Order/show',array('order_sn'=>$rs['order_sn']),true,true),$content);

//						dump($ss);


						$this->success("发货完成！", U("index"));
					} else {
						$this->error("保存失败！");
					}
				} else {
					$this->error($this->db->getError());
				}
		
			}else{
				$id=  intval(I("get.id"));
		
		
		
				$rs=$this->db->where("id=$id")->find();
		
				if($rs[status==1]){
					
					$this->error("该订单还未支付");
					
				}
				if($rs[status==3]){
						
					$this->error("该订单已经发货");
						
				}
				
					
				$this->assign("_data",$rs);
					
					
				$this->display("");
			}
		
		
		
		
		
		
		
		
	}



	public  function create_danju(){

	}

	

    
    
    public  function ifnew(){
    
    	$count = $this->db->where(array("status"=>2,"is_read"=>"0","modal"=>array("neq","chongzhi")))->count();
    
    
    	echo $count;
    
    
    }
    
    public function fankui(){
    	
    	
    	$phone=I("get.phone");
    	
    	$this->assign("phone",$phone);
    	
    	if($phone) $map['phone']=$phone;
    	
    	$map['status']=3;
    	$map['desc']=array("NEQ","");
    	
    	$list=$this->db->where($map)->select();
    	
    	
    	$this->assign("list",$list);
    	
    	
    	$this->display("");
    	
    	
    	
    }



	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));

			$order_sn=$this->db->where(array("id"=>$id))->getField("order_sn");

			$p_list=D("OrderInfo")->where(array("order_sn"=>$order_sn))->select();


			foreach($p_list as $k=>$v){


				M("Product")->where(array("id"=>$v[product_id]))->save(array("kucun"=>1)); //

			}



			if ($this->db->delete($id)) {
				D("OrderInfo")->where(array("order_sn"=>$order_sn))->delete();
				D("OrderPingjia")->where(array("order_sn"=>$order_sn))->delete();





				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}

	}
	

	
	
}